import { set } from 'lodash-es'

/**
 * 递归文件下的所有json文件里的内容并组合成对象
 * @param langs 语言文件
 * @returns
 */
export function genMessage(langs: Recordable<Recordable>) {
  const obj: Recordable = {}
  Object.keys(langs).forEach((key) => {
    const langFileModule = langs[key].default

    let fileName = key
      .replace(/^\.\//, '')
      .replace('/src/', '')
      .replace(/locale\/lang\/[^\/]*\//, '')

    const lastIndex = fileName.lastIndexOf('.')
    fileName = fileName.substring(0, lastIndex)
    const keyList = fileName.split('/')
    const moduleName = keyList.shift()
    const objKey = keyList.join('.')

    if (moduleName) {
      if (objKey) {
        set(obj, moduleName, obj[moduleName] || {})
        set(obj[moduleName] as object, objKey, langFileModule)
      } else {
        set(obj, moduleName, langFileModule || {})
      }
    }
  })
  return obj
}
